

/*
 * Main.java
 *
 * 
 */
package ifs.RnD.svnRead;

import de.javasoft.plaf.synthetica.SyntheticaBlackMoonLookAndFeel;

import ifs.RnD.Graph.Favourite;
import java.awt.Container;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.tmatesoft.svn.core.SVNException;

/**
 * Take initial inputs, repository path url and credentials
 * 
 */
public class Main extends javax.swing.JFrame {

    DisplayRepositoryTree drt;
    String url, name, password, fowordURL;
    int tag;
    char pword[];
    Compiler comp;
    private Container cp;
    public Favourite fav;
    public ArrayList<String> displayURLs;
    public boolean authst = false;

    /** Creates new form Main
     * @throws UnsupportedLookAndFeelException
     * @throws ParseException
     */
    public Main(String fowordURL,int tag) throws UnsupportedLookAndFeelException, ParseException {
        initComponents();
        jComboBox1.setEditable(true);
        displayURLs = new ArrayList<String>();
        this.fowordURL = fowordURL;
        this.tag = tag;
        fav = new Favourite();

        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        ok = new javax.swing.JButton();
        uName = new javax.swing.JTextField();
        pWord = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jToggleButton1 = new javax.swing.JToggleButton();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Authentication");
        setBackground(new java.awt.Color(255, 51, 51));
        setFont(new java.awt.Font("Tahoma", 0, 12));
        setResizable(false);

        jLabel1.setText("URL");

        ok.setText("OK");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        jLabel2.setText("User Name");

        jLabel3.setText("Password");

        jComboBox1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jToggleButton1.setText("Cancel");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/Login-icon.png"))); // NOI18N
        jLabel4.setPreferredSize(new java.awt.Dimension(90, 90));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(51, 51, 51)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(pWord, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(uName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(ok, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)
                        .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(uName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(pWord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(ok)
                                    .addComponent(jToggleButton1))))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    /* pop-ups a new logging box to log in to a to new new repository path */
    private void okActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            url = jComboBox1.getSelectedItem().toString();
            if (url.length() == 0) {
                JOptionPane.showMessageDialog(rootPane, "please enter the Repository URL !", "Empty URL", 1);
                reset();
            } else {
                name = uName.getText();
                pword = pWord.getPassword();
                password = convertPassword(pword);
                if (name.length() == 0 || password.length() == 0) {
                    JOptionPane.showMessageDialog(rootPane, "please enter the User Name and Password !", "Error Login", 1);
                } else {
                    drt = new DisplayRepositoryTree(url, name, password, this, fav);
                    if (authst) {
                        drt.processLink(url, drt);
                        this.setVisible(false);
                    }
                }
            }
        } catch (SVNException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    public void setURL() {
        displayURLs.clear();
        if(tag == 1){
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel((fav.readFileForMain(displayURLs)).toArray()));
        }
        else if(tag == 2){
            displayURLs.add(fowordURL);
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel((fav.readFileForMain(displayURLs)).toArray()));
        }
        
    }

    /**
     * converting the password char[] in to a string
     * @param cPassword
     * @return
     */
    public String convertPassword(char[] cPassword) {
        // Declare variables
        String strRet = new String("");

        // Go through each character
        for (int i = 0; i < cPassword.length; i++) {
            strRet += cPassword[i];
        }

        return strRet;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(new SyntheticaBlackMoonLookAndFeel() {

                        
                    });

                    Main mmm = new Main(null,1);
                    mmm.setURL();
                    mmm.setVisible(true);

                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ParseException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });

    }

    /**
     *
     * @return
     */
    public DisplayRepositoryTree getREpoTreeInstance() {
        return drt;
    }

    /**
     * setting name field and password field empty 
     */
    public void reset() {
        uName.setText("");
        pWord.setText("");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JButton ok;
    private javax.swing.JPasswordField pWord;
    private javax.swing.JTextField uName;
    // End of variables declaration//GEN-END:variables
}
